package com.marketlive.app.b2c;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.marketlive.app.b2c.common.constants.RequestParams;

/**
 * This class contains P2P navigation state, including the current category and the current product. The productSet is
 * the list of products that form part of the current navigation set and is used to determine where in a set of
 * next/previous products a user currently is, as well as the list of products to include in the pages of a directory
 * listing. The current product set will often be the same as the products in the current category - although they must
 * be set independently.
 */
public class NavState {

    /**
     * Logger
     */
    private static Log log = LogFactory.getLog(NavState.class);

    /** Possible nav state contexts. */
    public static final String SITE_SEARCH = "SiteSearch";
    public static final String DIRECTORY = "Directory";

    /** The context that the nav state is in. */
    private String currentContext;


    /** Maintained for Bread crumbs. */
    private List currentCategoryPath;
    
    private String displayConditionParams;

    int currentProductIndex;
    int currentPage;
    int currentTotalPageCount;
    boolean showAllProducts = false;

    String currentSortSelection;

    boolean listHasFamilies = false;

    /*******************************************************************************************************************
     * NavStateHelper will set to true if a redirect should be done after loading the navstate from the URL. Example
     * case: invalid category path replaced with default
     ******************************************************************************************************************/
    boolean shouldRedirect = false;

    /**
     * Tells CategoryNavController whether or not to hightlight the current category. Almost all P2P pages with a valid
     * NavState will want this true. Basket is an example where there is a valid NavState, but don't want category
     * highlighted.
     */
    boolean highlightCategory = true;

    /**
     * "Starting" page of a multi-page P2P interaction.
     */
    private String from;
    /**
     * Indicates whether the navigation is driven by search or not.
     */
    private boolean searchNavigation = false;

    // Extended page type suffix on /product URI
    String pageTypeFromURI;

    public List getCurrentCategoryPath() {
        return currentCategoryPath;
    }

    public int getCurrentProductIndex() {
        return currentProductIndex;
    }

    public void setCurrentProductIndex(int currentProductIndex) {
        if (currentProductIndex < 0) {
            this.currentProductIndex = 0;
        } else {
            this.currentProductIndex = currentProductIndex;
        }

        log.debug("setCurrentProductIndex( " + currentProductIndex + " )");
    }

    /**
     * Returns the current page within the current category.
     *
     * @return int
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * Setter for currentPage property.
     *
     * @param currentPage Current category page.
     */
    public void setCurrentPage(int currentPage) {

        log.debug("setCurrentPage( " + currentPage + " )");

        this.currentPage = currentPage;
    }

    /**
     * Returns total number of pages for current category.
     *
     * @return int.
     */
    public int getCurrentTotalPageCount() {
        return currentTotalPageCount;
    }

    /**
     * Setter for currentTotalPageCount property.
     *
     * @param currentTotalPageCount Total number of pages for current category.
     */
    public void setCurrentTotalPageCount(int currentTotalPageCount) {

        log.debug("setCurrentTotalPageCount( " + currentTotalPageCount + " )");

        this.currentTotalPageCount = currentTotalPageCount;
    }

    public String getCurrentSortSelection() {
        return currentSortSelection;
    }

    public void setCurrentSortSelection(String currentSortSelection) {
        log.debug("setCurrentSortSelection( " + currentSortSelection + " )");
        this.currentSortSelection = currentSortSelection;
    }

    /**
     * Retuens a flag indicating if a user selected to view all the products.
     *
     * @return boolean
     */
    public boolean isShowAllProducts() {
        return showAllProducts;
    }

    /**
     * Setter for showAllProducts property.
     *
     * @param showAllProducts
     */
    public void setShowAllProducts(boolean showAllProducts) {

        log.debug("setShowAllProducts( " + showAllProducts + " )");

        this.showAllProducts = showAllProducts;
    }

    /**
     * Set this to true if category item list contains at least one family.
     * @param listHasFamilies
     */
    public void setListHasFamilies(boolean listHasFamilies) {

        log.debug("setListHasFamilies( " + listHasFamilies + " )");

        this.listHasFamilies = listHasFamilies;
    }

    /**
     * Should the action redirect after the navstate loaded from URL?
     *
     * @return
     */
    public boolean isShouldRedirect() {
        return shouldRedirect;
    }

    /**
     * Sets the flag whether the action redirect after the navstate loaded from URL
     */
    public void setShouldRedirect(boolean shouldRedirect) {
        this.shouldRedirect = shouldRedirect;
    }

    /**
     * Should the current category be hightlighted in the category nav menu?
     *
     * @return
     */
    public boolean isHighlightCategory() {
        return highlightCategory;
    }

    /**
     * Sets the flag controlling whether the current category should be hightlighted in the category nav menu.
     */
    public void setHighlightCategory(boolean highlightCategory) {
        this.highlightCategory = highlightCategory;
    }

    /**
     * Current context state (ie. Directory or Search)
     *
     * @return
     */
    public String getCurrentContext() {
        return currentContext;
    }

    /**
     * Set the context that the nav state is in -- Site Search, Directory, etc.
     * @param currentContext
     */
    public void setCurrentContext(String currentContext) {

        log.debug("setCurrentContext( " + currentContext + " )");

        this.currentContext = currentContext;
    }

    /**
     *
     * @return
     */
    public boolean isDirectory() {
        return currentContext != null && currentContext.equals(DIRECTORY);
    }

    /**
     * Returns originating page for a multi-request form submission.
     * @return
     */
    public String getFrom() {

        return from;
    }

    /**
     * Sets originating page for a multi-request form submission.
     * @param from the originating page for a multi-request form submission
     */
    public void setFrom(String from) {

        log.debug("setFrom( " + from + " )");

        this.from = from;
    }

    public boolean isSearchNavigation(){
    	return searchNavigation;
    }
    
    public void setSearchNavigation(boolean searchNavigation){
    	this.searchNavigation = searchNavigation;
    }
    
    /**
     *
     * @return
     */
    public String getPageTypeFromURI() {
        return pageTypeFromURI;
    }

    /**
     *
     * @param pageTypeFromURI
     */
    public void setPageTypeFromURI(String pageTypeFromURI) {

        log.debug("setPageTypeFromURI( " + pageTypeFromURI + " )");

        this.pageTypeFromURI = pageTypeFromURI;
    }

    public String getDisplayConditionParams() {
        return displayConditionParams;
    }

    public void setDisplayConditionParams(String displayConditionParams) {
        this.displayConditionParams = displayConditionParams;
    }
}
